使用 R 的 xlsx 包创建 XLSX 文件时,默认情况下,包含字符串的列默认向左对齐,包含整数的列向右对齐(包含整数和字符串混合的列也向左对齐)。最终,我想通过将所有列全部向左对齐来标准化所有列,但我在使用 xlsx 时遇到困难。使用下面的示例,如何将所有单元格向左对齐?
library(xlsx)
# Creating dataframe.
df <- data.frame(c(1, 2, 3),
c("one", "two", "three"),
c("1", "2", "3"))
# Creating a workbook using the XLSX package.
wb <- xlsx::createWorkbook(type = "xlsx")
# Creating a sheet inside the workbook.
sheet <- xlsx::createSheet(wb, sheetName = "Sheet0")
# Adding the full dataset into the sheet.
xlsx::addDataFrame(df, sheet, startRow = 1, startCol = 1, row.names = FALSE, col.names = FALSE)
# Saving the workbook.
xlsx::saveWorkbook(wb, "df.xlsx")
Run Code Online (Sandbox Code Playgroud) 我正在尝试根据其值更改 R Shiny 数据表的单元格颜色。例如,我创建了以下应用程序:
# ui.R
fluidPage(
# Outputting data table.
DT::dataTableOutput("table")
)
# server.R
library(DT)
data(iris)
function(input, output) {
# Rendering data table.
output$table <- DT::renderDataTable({
head(iris)
},
options = list(dom = "t",
ordering = FALSE))
}
Run Code Online (Sandbox Code Playgroud)
以下是从上述代码生成的 HTML 框架和结果页面:
例如,假设我希望所有包含整数的单元格都涂成红色。有选择地,我只想为第 2 行第 2 列和第 5 行第 1 列的单元格着色,其中值分别为 3 和 5。这在 R Shiny 中可能吗?
我目前的解决方法是在服务器端设置单个单元格的类,然后用 CSS 为它们着色。但是,我找不到办法做到这一点。
我在 R Shiny 应用程序中将变量从模块返回到服务器时遇到了令人惊讶的困难。在模块中,我想在观察到按下按钮时返回一个值,因此我将return()语句包装在observeEvent(). 但是,没有返回所需的值,整个observeEvent()块似乎是。
我试图创建一个最小的工作示例,概述以下问题:
# ui.R
fluidPage(
input_module_ui("input"),
actionButton("print_input_button",
label = "Print Input")
)
Run Code Online (Sandbox Code Playgroud)
# server.R
function(input, output, session) {
# Calling input module.
input_module_return <- callModule(input_module, "input")
observeEvent(input$print_input_button, {
print(input_module_return)
})
}
Run Code Online (Sandbox Code Playgroud)
# global.R
source("modules/input.R")
Run Code Online (Sandbox Code Playgroud)
# input.R
input_module_ui <- function(id) {
ns <- NS(id)
tagList(
textInput(ns("text_input"),
label = h2("Input Text:")),
actionButton(ns("submit_input"),
label = "Submit Input")
)
}
input_module <- function(input, output, session) {
print("I should only print …Run Code Online (Sandbox Code Playgroud)