【发布时间】:2019-11-26 16:54:14
【问题描述】:
我正在尝试做的是允许用户将配置/查找 excel 表传递给闪亮,以闪亮的方式显示此表,允许用户以闪亮的方式编辑单元格,并使用已编辑的值从可编辑的表格中进行计算。我的问题出现在最后一步“使用从可编辑表中编辑的值进行计算”。
excel 文件由 2 个选项卡组成,其中包含以下内容的数据:
Tab1 名称:“参数” data.frame(Name = c("a", "b", "c"), Value = c(1:3))
Tab2 名称:“parameters2” data.frame(Name = c("a", "b", "c"), Value = c(4:6))
理想的闪亮应用应该做到以下几点:
1) 在上传时,执行一个计算,将 Tab 1 和 Tab 2 的第一个值相加。这将是 1 + 4 = 5。
2) 如果用户将 Tab 1 的值 1 修改为 8,则计算结果为 8 + 4 = 12。
实际上,如果用户对其进行任何编辑,我想使用已编辑的表值来更新我的所有计算。我知道这可以通过简单地以闪亮的方式上传一个新文件来完成,但我宁愿让他们以闪亮的方式做到这一点,而不是上传一个新文件。
这是我闪亮的应用程序。感谢任何帮助/指导!
library(shiny)
library(DT)
shinyApp(
ui <- fluidPage(
fileInput(inputId = "config", label = "Upload Configuration File",
multiple = F, accept = c(".xlsx", ".xls")),
verbatimTextOutput("txt"),
tagList(tags$head(tags$style(type = 'text/css','.navbar-brand{display:none;}')),
navbarPage(title = "",
tabPanel(title = "Parameters",
dataTableOutput(outputId = "edit.param", width = 2)),
tabPanel(title = "Parameters2",
dataTableOutput(outputId = "edit.param2", width = 2))
)
)
),
server = function(input, output, session) {
config.path = reactive({
inFile = input$config
if(is.null(inFile)) {
return(NULL)
} else {
return(inFile$datapath)
}
})
df.param = reactive({
read_excel(path = config.path(), sheet = "parameters")
})
df.param2 = reactive({
read_excel(path = config.path(), sheet = "parameters2")
})
output$edit.param = renderDT(df.param(), selection = "none", server = F, editable = "cell")
output$edit.param2 = renderDT(df.param2(), selection = "none", server = F, editable = "cell")
observeEvent(input$edit.param_cell_edit, {
df.param()[input$edit.param_cell_edit$row, input$edit.param_cell_edit$col] <<- input$edit.param_cell_edit$value
})
observeEvent(input$edit.param2_cell_edit, {
df.param()[input$edit.param2_cell_edit$row, input$edit.param2_cell_edit$col] <<- input$edit.param2_cell_edit$value
})
output$txt = reactive({
df.param()$value[1] + df.param2()$value[1]
})
}
)
我也在服务器部分尝试过这个,但也没有运气:
output$edit.param = renderDT(df.param(), selection = "none", server = F, editable = "cell")
output$edit.param2 = renderDT(df.param2(), selection = "none", server = F, editable = "cell")
observe(input$edit.param_cell_edit)
observe(input$edit.param2_cell_edit)
【问题讨论】:
-
您应该避免在 id 中使用点(例如
edit.param)。但你应用的主要问题是output$txt,应该是output$txt = renderPrint({df.param()$value[1] + df.param2()$value[1]})。 -
@StéphaneLaurent 点在 R 中是相当标准的。在 Shiny 中使用它们有问题吗?我没有遇到任何问题。 renderPrint 很好,但这仍然不能解决我的问题。
-
Javascript 中的点在某种程度上类似于 R 中的
$。例如,如果您在conditionalPanel的 Javascript 条件中使用带点的 id,则会出现问题,因为,例如,如果id 是id.dot,那么 Javascript 中的input.id.dot被解释为input的id元素的dot元素。你遇到什么问题?有错误信息吗? -
@StéphaneLaurent 我没有任何错误或警告消息。我的主要问题是如何在编辑闪亮的表格后传递计算值。我知道如何使表格可编辑,但我不知道如何将这些编辑后的值传递给计算。例如,我上传了一个 alpha = 1 的文件,我的初始计算将是 e^(1) = 2.72。现在,如果我将其编辑为闪亮的 alpha = 2,我的新计算应该是 e^(2) =7.39。