闪亮的 rhandsontable 自动值取决于用户
Posted
技术标签:
【中文标题】闪亮的 rhandsontable 自动值取决于用户【英文标题】:Shiny rhandsontable automatic values depending on User 【发布时间】:2022-01-16 20:56:29 【问题描述】:我有一个表,用户将在其中输入一些组。因此,我希望另一列自动更新并显示每个组的频率(或重复):
这段代码创建了这个应用程序:
library(shiny)
library(rhandsontable)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Automatic data rhandsontable"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
rhandsontable::rHandsontableOutput('ed_out'),
shiny::actionButton('start_input', 'save final table')
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output)
# This has to be reactive
data <- reactive(
df <- data.frame(Animal = c('Dog', 'Cat', 'Mouse', 'Elephant', 'Tiger'),
Group = ' ',
replicate = as.numeric(' '))
)
output$ed_out <- rhandsontable::renderRHandsontable(
df <- data()
rhandsontable(
df,
height = 500,
width = 600) %>%
hot_col('replicate', format = '0a', readOnly = TRUE) %>%
hot_col('Animal', readOnly = TRUE)
)
# This is just to save the table when the user has finished, can be ignored
group_finals <- reactiveValues()
observeEvent(input$start_input,
group_finals$data <- rhandsontable::hot_to_r(input$ed_out)
print(group_finals$data)
)
# Run the application
shinyApp(ui = ui, server = server)
所以这个想法是用户输入组和复制自动更新:(这里用户输入B, B, A, A, B
。
我能够计算每个组的重复数,但我不确定如何实现这部分来计算它们并在用户输入每个组后同时显示它们。
df <- df %>%
group_by(Group) %>%
mutate(replicate = 1:n())
不确定这是否是最好的方法,我尝试使用hot_to_col
renderer
来使用javascript
,但我不熟悉那种语言。
【问题讨论】:
【参考方案1】:抱歉,我不熟悉 tidyverse - 所以我切换到 data.table
。
hot_to_r
是正确的方法:
library(shiny)
library(rhandsontable)
library(data.table)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Automatic data rhandsontable"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
rhandsontable::rHandsontableOutput('ed_out'),
shiny::actionButton('start_input', 'save final table')
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output)
# This has to be reactive
data <- reactive(
data.frame(Animal = c('Dog', 'Cat', 'Mouse', 'Elephant', 'Tiger'),
Group = '',
replicate = NA_integer_)
)
myData <- reactiveVal()
observeEvent(data(),
myData(data())
)
output$ed_out <- rhandsontable::renderRHandsontable(
rhandsontable(
myData(),
height = 500,
width = 600) %>%
hot_col('replicate', format = '0a', readOnly = TRUE) %>%
hot_col('Animal', readOnly = TRUE)
)
observeEvent(input$ed_out,
userDT <- rhandsontable::hot_to_r(input$ed_out)
setDT(userDT)
userDT[, replicate := seq_len(.N), by = Group][is.na(Group) | Group == "", replicate := NA_integer_]
myData(userDT)
)
# This is just to save the table when the user has finished, can be ignored
group_finals <- reactiveValues()
observeEvent(input$start_input,
group_finals$myData <- rhandsontable::hot_to_r(input$ed_out)
print(group_finals$myData)
)
# Run the application
shinyApp(ui = ui, server = server)
【讨论】:
非常感谢,但我在实现它时遇到了麻烦,因为数据,原始文件本身已经是一个反应元素(取决于其他东西)。所以我不能做 myData @CodingBiology 您需要同步reactive
和reactiveVal
。查看我的编辑。
非常感谢!工作得很好:)顺便说一句,你花了多长时间才达到那种闪亮的水平?以上是关于闪亮的 rhandsontable 自动值取决于用户的主要内容,如果未能解决你的问题,请参考以下文章
闪亮:覆盖rhandsontable,赋值左侧无效(NULL)
Shiny and rhandsontable - 基于列总和的条件单元格/列格式