如何在 R Shiny Flexdashboard 中覆盖(点击 actionButton 时)反应对象
How to override (upon actionButton click) a reactive object in R Shiny Flexdashboard
我正在尝试制作一个 R Shiny Flexdashboard,它允许用户使用 本地 Excel 文件 或 下载了 Googlesheet,作为数据源。
我在这里创建了一个示例,但我使用本地 csv 文件代替 Googlesheets 下载组件(在示例中使用 googlesheets 会很复杂)。
https://gist.github.com/jtag04/67ae6b2c39e4f68f90e06bb1ce2ceb98
上面的脚本有效(将其另存为 *.rmd 文件以便 运行 - 它是一个 Flexdashboard)。
但是,我的挑战是我希望 *csv 上传(当按下 actionButton 时)覆盖 Excel 文件对象(如果存在)。
即不是将 csv 保存到 step_one_2(如示例中所示),而是覆盖 step_one 对象(如果存在)。
这可能吗?
非常感谢。
实现类似目标的一种方法(如果我理解正确的话)是使用 radioButtons
让用户选择输入类型。然后根据用户的选择呈现不同的输入元素,并有一个反应来适当地处理两种不同的输入文件类型。最后,我们从 excel 或 googlesheets 文件中渲染单个 table。
代码:
library(shiny)
library(tidyverse)
# let the user choose the type of input file
radioButtons(
inputId = "what_source",
label = "Choose source file",
choices = c("googlesheets", "excel"),
inline = FALSE
)
# render file input widgets conditioned on user choice
conditionalPanel(condition = 'input.what_source == "googlesheets"',
fileInput("google_file", "Upload from csv", accept = ".csv")
)
conditionalPanel(condition = 'input.what_source == "excel"',
fileInput("excel_file", "Upload from excel", accept = ".xlsx")
)
# deal with the file selected by the user
what_file <- reactive({
if (input$what_source == "googlesheets") {
x <- readr::read_csv(input$google_file$datapath) %>% mutate(source = "csv")
} else {
x <- readxl::read_excel(input$excel_file$datapath) %>% mutate(source = "Excel")
return(x)
}
})
# then in a different element downstream add the render function:
renderDataTable(what_file())
更新:随意获取csv,但改为excel带复选框的输入:
服务器逻辑:
# fetch updated csv data
actionButton("fetch", "Fetch new data")
checkboxInput("excel", label = "Get excel file", value = FALSE)
fetch_new <- eventReactive(input$fetch, {
readr::read_csv("df.csv")
})
conditionalPanel(condition = 'input.excel == true',
fileInput("excel_file",
"Upload from excel",
accept = ".xlsx")
)
what_file <- reactive({
if (!input$excel) {
x <- fetch_new()
} else {
x <- readxl::read_excel(input$excel_file$datapath)
return(x)
}
})
我正在尝试制作一个 R Shiny Flexdashboard,它允许用户使用 本地 Excel 文件 或 下载了 Googlesheet,作为数据源。
我在这里创建了一个示例,但我使用本地 csv 文件代替 Googlesheets 下载组件(在示例中使用 googlesheets 会很复杂)。
https://gist.github.com/jtag04/67ae6b2c39e4f68f90e06bb1ce2ceb98
上面的脚本有效(将其另存为 *.rmd 文件以便 运行 - 它是一个 Flexdashboard)。
但是,我的挑战是我希望 *csv 上传(当按下 actionButton 时)覆盖 Excel 文件对象(如果存在)。
即不是将 csv 保存到 step_one_2(如示例中所示),而是覆盖 step_one 对象(如果存在)。
这可能吗? 非常感谢。
实现类似目标的一种方法(如果我理解正确的话)是使用 radioButtons
让用户选择输入类型。然后根据用户的选择呈现不同的输入元素,并有一个反应来适当地处理两种不同的输入文件类型。最后,我们从 excel 或 googlesheets 文件中渲染单个 table。
代码:
library(shiny)
library(tidyverse)
# let the user choose the type of input file
radioButtons(
inputId = "what_source",
label = "Choose source file",
choices = c("googlesheets", "excel"),
inline = FALSE
)
# render file input widgets conditioned on user choice
conditionalPanel(condition = 'input.what_source == "googlesheets"',
fileInput("google_file", "Upload from csv", accept = ".csv")
)
conditionalPanel(condition = 'input.what_source == "excel"',
fileInput("excel_file", "Upload from excel", accept = ".xlsx")
)
# deal with the file selected by the user
what_file <- reactive({
if (input$what_source == "googlesheets") {
x <- readr::read_csv(input$google_file$datapath) %>% mutate(source = "csv")
} else {
x <- readxl::read_excel(input$excel_file$datapath) %>% mutate(source = "Excel")
return(x)
}
})
# then in a different element downstream add the render function:
renderDataTable(what_file())
更新:随意获取csv,但改为excel带复选框的输入:
服务器逻辑:
# fetch updated csv data
actionButton("fetch", "Fetch new data")
checkboxInput("excel", label = "Get excel file", value = FALSE)
fetch_new <- eventReactive(input$fetch, {
readr::read_csv("df.csv")
})
conditionalPanel(condition = 'input.excel == true',
fileInput("excel_file",
"Upload from excel",
accept = ".xlsx")
)
what_file <- reactive({
if (!input$excel) {
x <- fetch_new()
} else {
x <- readxl::read_excel(input$excel_file$datapath)
return(x)
}
})