考慮這個函式:
fx <- function(x) {
sapply(X = x, FUN = function(x) {
if (x > 1) {
NULL
} else (x)
})
}
而這個資料:
set.seed(5)
df_1 <- data.frame(x = replicate(n = 5, expr = runif(n = 5, min = 0, max = 1.7)))
現在,我將該函式應用于此資料的一列:
fx(df_1[,1])
現在,我在一行中應用該功能。
fx(df_1[1,])
但是,當我嘗試在里面應用它時,reactiveValues我無法讓它作業。我想我把它錯誤地放在里面observeEvent。
我想,當用戶輸入大于1的值時(在一行或一列或兩者都選中),單元格再次為空,就像輸入文本時一樣(您可以通過在中寫入文本來測驗它,你會看到它變成了空的)。
我的DT:
library(shiny)
library(shinydashboard)
library(tidyverse)
header <- dashboardHeader(title = "Dash", titleWidth = 250)
sidebar <- dashboardSidebar(sidebarMenu(menuItem(text = "Testar", tabName = "test")))
body <- dashboardBody(
tabItems(
tabItem(
tabName = "test",
titlePanel("Tabela"),
fluidPage(
column(
width = 3,
DT::dataTableOutput("my_datatable")
)
)
)
)
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
v <- reactiveValues(data = {
data.frame(y = rep(NA, 30), x = rep(NA, 30))
})
output$my_datatable <- DT::renderDataTable({
DT::datatable(
data = v$data,
editable = TRUE,
rownames = TRUE,
selection = list(mode = 'none'),
options = list(
searching = FALSE,
paging = FALSE,
ordering = FALSE,
info = FALSE,
autoWidth = TRUE
)
)
})
observeEvent(input$my_datatable_cell_edit, {
info = input$my_datatable_cell_edit
i = as.numeric(info$row)
j = as.numeric(info$col)
k = as.numeric(info$value)
# converter positivos em negativos (se character, o resultado e NULL)
if (!is.na(k) & k < 0) {
k <- k * -1
} else (k)
v$data[i, j] <- k
})
}
shinyApp(ui, server)
我在這個問題的開頭寫的函式我在這個observeEvent下面使用了:
if (!is.na(k) & k < 0) {
k <- k * -1
} else (k)
哪個效果很好(我想保留它)。但是,我想在特定的列和行上添加一些額外的條件,這是我無法做到的(每次我嘗試,DT崩潰并且應用程式崩潰)。
uj5u.com熱心網友回復:
您的函式有兩個問題會導致錯誤。首先,您將 value 替換為,NULL以便您的函式回傳一個串列而不是一個向量。其次,您的函式不考慮NA資料表中的 s,即當您遍歷向量元素時,每個 NA 元素在檢查時都會產生錯誤if (x > 1)。實際上,您可以更簡潔地重寫您的函式,而無需像這樣運行這些問題:
fx <- function(x) {
x[x > 1] <- NA
x
}
注意:恕我直言,不需要替換行或列中的值的函式。當用戶輸入每個單元格的值時,檢查輸入的值是否大于 1 并替換它就足夠了NA,以防萬一,即您可以使用它if (k > 1) k <- NA。
fx <- function(x) {
x[x > 1] <- NA
x
}
library(shiny)
library(shinydashboard)
header <- dashboardHeader(title = "Dash", titleWidth = 250)
sidebar <- dashboardSidebar(sidebarMenu(menuItem(text = "Testar", tabName = "test")))
body <- dashboardBody(
tabItems(
tabItem(
tabName = "test",
titlePanel("Tabela"),
fluidPage(
column(
width = 3,
DT::dataTableOutput("my_datatable")
)
)
)
)
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
v <- reactiveValues(data = {
data.frame(y = rep(NA, 30), x = rep(NA, 30))
})
output$my_datatable <- DT::renderDataTable({
DT::datatable(
data = v$data,
editable = TRUE,
rownames = TRUE,
selection = list(mode = "none"),
options = list(
searching = FALSE,
paging = FALSE,
ordering = FALSE,
info = FALSE,
autoWidth = TRUE
)
)
})
observeEvent(input$my_datatable_cell_edit, {
info <- input$my_datatable_cell_edit
i <- as.numeric(info$row)
j <- as.numeric(info$col)
k <- as.numeric(info$value)
if (!is.na(k) && k < 0) {
k <- k * -1
}
v$data[i, j] <- k
v$data[i, ] <- fx(v$data[i, ])
})
}
shinyApp(ui, server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/513421.html
標籤:r闪亮的闪亮的仪表板dt
上一篇:如何在R中的列序列中重新編碼值
